{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2760a950",
   "metadata": {},
   "source": [
    "-----\n",
    "* 2023-10-16\n",
    "* 课程：python语言\n",
    "* 编写人：梁思琦\n",
    "* week06\n",
    "-----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bbffb1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [  1, 3, 5, 7,9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ffaf7e78",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7a4a345",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ed6cce15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on int object:\n",
      "\n",
      "class int(object)\n",
      " |  int([x]) -> integer\n",
      " |  int(x, base=10) -> integer\n",
      " |  \n",
      " |  Convert a number or string to an integer, or return 0 if no arguments\n",
      " |  are given.  If x is a number, return x.__int__().  For floating point\n",
      " |  numbers, this truncates towards zero.\n",
      " |  \n",
      " |  If x is not a number or if base is given, then x must be a string,\n",
      " |  bytes, or bytearray instance representing an integer literal in the\n",
      " |  given base.  The literal can be preceded by '+' or '-' and be surrounded\n",
      " |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\n",
      " |  Base 0 means to interpret the base from the string as an integer literal.\n",
      " |  >>> int('0b100', base=0)\n",
      " |  4\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      bool\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __abs__(self, /)\n",
      " |      abs(self)\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __and__(self, value, /)\n",
      " |      Return self&value.\n",
      " |  \n",
      " |  __bool__(self, /)\n",
      " |      True if self else False\n",
      " |  \n",
      " |  __ceil__(...)\n",
      " |      Ceiling of an Integral returns itself.\n",
      " |  \n",
      " |  __divmod__(self, value, /)\n",
      " |      Return divmod(self, value).\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __float__(self, /)\n",
      " |      float(self)\n",
      " |  \n",
      " |  __floor__(...)\n",
      " |      Flooring an Integral returns itself.\n",
      " |  \n",
      " |  __floordiv__(self, value, /)\n",
      " |      Return self//value.\n",
      " |  \n",
      " |  __format__(self, format_spec, /)\n",
      " |      Default object formatter.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __index__(self, /)\n",
      " |      Return self converted to an integer, if self is suitable for use as an index into a list.\n",
      " |  \n",
      " |  __int__(self, /)\n",
      " |      int(self)\n",
      " |  \n",
      " |  __invert__(self, /)\n",
      " |      ~self\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __lshift__(self, value, /)\n",
      " |      Return self<<value.\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mod__(self, value, /)\n",
      " |      Return self%value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __neg__(self, /)\n",
      " |      -self\n",
      " |  \n",
      " |  __or__(self, value, /)\n",
      " |      Return self|value.\n",
      " |  \n",
      " |  __pos__(self, /)\n",
      " |      +self\n",
      " |  \n",
      " |  __pow__(self, value, mod=None, /)\n",
      " |      Return pow(self, value, mod).\n",
      " |  \n",
      " |  __radd__(self, value, /)\n",
      " |      Return value+self.\n",
      " |  \n",
      " |  __rand__(self, value, /)\n",
      " |      Return value&self.\n",
      " |  \n",
      " |  __rdivmod__(self, value, /)\n",
      " |      Return divmod(value, self).\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __rfloordiv__(self, value, /)\n",
      " |      Return value//self.\n",
      " |  \n",
      " |  __rlshift__(self, value, /)\n",
      " |      Return value<<self.\n",
      " |  \n",
      " |  __rmod__(self, value, /)\n",
      " |      Return value%self.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  __ror__(self, value, /)\n",
      " |      Return value|self.\n",
      " |  \n",
      " |  __round__(...)\n",
      " |      Rounding an Integral returns itself.\n",
      " |      \n",
      " |      Rounding with an ndigits argument also returns an integer.\n",
      " |  \n",
      " |  __rpow__(self, value, mod=None, /)\n",
      " |      Return pow(value, self, mod).\n",
      " |  \n",
      " |  __rrshift__(self, value, /)\n",
      " |      Return value>>self.\n",
      " |  \n",
      " |  __rshift__(self, value, /)\n",
      " |      Return self>>value.\n",
      " |  \n",
      " |  __rsub__(self, value, /)\n",
      " |      Return value-self.\n",
      " |  \n",
      " |  __rtruediv__(self, value, /)\n",
      " |      Return value/self.\n",
      " |  \n",
      " |  __rxor__(self, value, /)\n",
      " |      Return value^self.\n",
      " |  \n",
      " |  __sizeof__(self, /)\n",
      " |      Returns size in memory, in bytes.\n",
      " |  \n",
      " |  __sub__(self, value, /)\n",
      " |      Return self-value.\n",
      " |  \n",
      " |  __truediv__(self, value, /)\n",
      " |      Return self/value.\n",
      " |  \n",
      " |  __trunc__(...)\n",
      " |      Truncating an Integral returns itself.\n",
      " |  \n",
      " |  __xor__(self, value, /)\n",
      " |      Return self^value.\n",
      " |  \n",
      " |  as_integer_ratio(self, /)\n",
      " |      Return integer ratio.\n",
      " |      \n",
      " |      Return a pair of integers, whose ratio is exactly equal to the original int\n",
      " |      and with a positive denominator.\n",
      " |      \n",
      " |      >>> (10).as_integer_ratio()\n",
      " |      (10, 1)\n",
      " |      >>> (-10).as_integer_ratio()\n",
      " |      (-10, 1)\n",
      " |      >>> (0).as_integer_ratio()\n",
      " |      (0, 1)\n",
      " |  \n",
      " |  bit_count(self, /)\n",
      " |      Number of ones in the binary representation of the absolute value of self.\n",
      " |      \n",
      " |      Also known as the population count.\n",
      " |      \n",
      " |      >>> bin(13)\n",
      " |      '0b1101'\n",
      " |      >>> (13).bit_count()\n",
      " |      3\n",
      " |  \n",
      " |  bit_length(self, /)\n",
      " |      Number of bits necessary to represent self in binary.\n",
      " |      \n",
      " |      >>> bin(37)\n",
      " |      '0b100101'\n",
      " |      >>> (37).bit_length()\n",
      " |      6\n",
      " |  \n",
      " |  conjugate(...)\n",
      " |      Returns self, the complex conjugate of any int.\n",
      " |  \n",
      " |  to_bytes(self, /, length=1, byteorder='big', *, signed=False)\n",
      " |      Return an array of bytes representing an integer.\n",
      " |      \n",
      " |      length\n",
      " |        Length of bytes object to use.  An OverflowError is raised if the\n",
      " |        integer is not representable with the given number of bytes.  Default\n",
      " |        is length 1.\n",
      " |      byteorder\n",
      " |        The byte order used to represent the integer.  If byteorder is 'big',\n",
      " |        the most significant byte is at the beginning of the byte array.  If\n",
      " |        byteorder is 'little', the most significant byte is at the end of the\n",
      " |        byte array.  To request the native byte order of the host system, use\n",
      " |        `sys.byteorder' as the byte order value.  Default is to use 'big'.\n",
      " |      signed\n",
      " |        Determines whether two's complement is used to represent the integer.\n",
      " |        If signed is False and a negative integer is given, an OverflowError\n",
      " |        is raised.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  from_bytes(bytes, byteorder='big', *, signed=False) from builtins.type\n",
      " |      Return the integer represented by the given array of bytes.\n",
      " |      \n",
      " |      bytes\n",
      " |        Holds the array of bytes to convert.  The argument must either\n",
      " |        support the buffer protocol or be an iterable object producing bytes.\n",
      " |        Bytes and bytearray are examples of built-in objects that support the\n",
      " |        buffer protocol.\n",
      " |      byteorder\n",
      " |        The byte order used to represent the integer.  If byteorder is 'big',\n",
      " |        the most significant byte is at the beginning of the byte array.  If\n",
      " |        byteorder is 'little', the most significant byte is at the end of the\n",
      " |        byte array.  To request the native byte order of the host system, use\n",
      " |        `sys.byteorder' as the byte order value.  Default is to use 'big'.\n",
      " |      signed\n",
      " |        Indicates whether two's complement is used to represent the integer.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  denominator\n",
      " |      the denominator of a rational number in lowest terms\n",
      " |  \n",
      " |  imag\n",
      " |      the imaginary part of a complex number\n",
      " |  \n",
      " |  numerator\n",
      " |      the numerator of a rational number in lowest terms\n",
      " |  \n",
      " |  real\n",
      " |      the real part of a complex number\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(datetime.datetime.today().year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51af737c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 补充random模块的介绍\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "badf3710",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7380363339576307\n",
      "0.41596255420896255\n",
      "0.3759702009886189\n",
      "0.20766483020587623\n",
      "0.6524605478664264\n",
      "0.8849993855510326\n",
      "0.4723469080055942\n",
      "0.11617014191488295\n",
      "0.9829157458764293\n",
      "0.8827244032303746\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b711b9d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "36\n",
      "68\n",
      "47\n",
      "75\n",
      "66\n",
      "71\n",
      "82\n"
     ]
    }
   ],
   "source": [
    "# 如果8次数值依次为:[45,34,1,78,61,34,25,23]\n",
    "for i in range(8):\n",
    "    print(int(random.random()*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47ddbcf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method randint in module random:\n",
      "\n",
      "randint(a, b) method of random.Random instance\n",
      "    Return random integer in range [a, b], including both end points.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8b364dcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "916371c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "2\n",
      "4\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "  print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2221bdf5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时钟的秒数为 44,我们将静止 1秒,此刻为偶数时间\n",
      "当前时钟的秒数为 45,时间将静止 3秒,此刻为奇数时间\n",
      "当前时钟的秒数为 48,我们将静止 4秒,此刻为偶数时间\n",
      "当前时钟的秒数为 52,我们将静止 3秒,此刻为偶数时间\n",
      "当前时钟的秒数为 55,时间将静止 1秒,此刻为奇数时间\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5): # for + range 可以决定程序执行的 次数\n",
    "    right_this_second = datetime.today().second #选择当前时钟的秒数\n",
    "    wait_time = random.randint(1,5)# 随机选择1-5之间的一个整数\n",
    "    if right_this_second in odds:# 如果当前的秒数为奇数则执行以下代码\n",
    "        print(\"当前时钟的秒数为 %s,时间将静止 %s秒,此刻为奇数时间\"%(right_this_second,wait_time))\n",
    "    else:\n",
    "        print(\"当前时钟的秒数为 %s,我们将静止 %s秒,此刻为偶数时间\"%(right_this_second,wait_time))\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e15050e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 项目三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ec7f6e39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99 bottle 在啤酒墙上~\n",
      "99 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "93 bottle 在啤酒墙上~\n",
      "93 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "87 bottle 在啤酒墙上~\n",
      "87 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "81 bottle 在啤酒墙上~\n",
      "81 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "75 bottle 在啤酒墙上~\n",
      "75 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "69 bottle 在啤酒墙上~\n",
      "69 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "63 bottle 在啤酒墙上~\n",
      "63 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "57 bottle 在啤酒墙上~\n",
      "57 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "51 bottle 在啤酒墙上~\n",
      "51 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "45 bottle 在啤酒墙上~\n",
      "45 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "39 bottle 在啤酒墙上~\n",
      "39 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "33 bottle 在啤酒墙上~\n",
      "33 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "27 bottle 在啤酒墙上~\n",
      "27 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "21 bottle 在啤酒墙上~\n",
      "21 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "15 bottle 在啤酒墙上~\n",
      "15 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "9 bottle 在啤酒墙上~\n",
      "9 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "只剩三瓶，不能拿了~\n",
      "3 bottle 在啤酒墙上~\n",
      "3 bottle 啤酒\n",
      "一次6瓶落\n",
      "取走一次\n",
      "没了没了，没有酒啦~\n"
     ]
    }
   ],
   "source": [
    "for beer_num in range(99,0,-6):\n",
    "    print(beer_num, \"bottle\", \"在啤酒墙上~\")\n",
    "    print(beer_num, \"bottle\", \"啤酒\")\n",
    "    print(\"一次6瓶落\")\n",
    "    print(\"取走一次\")\n",
    "    if beer_num == 3:\n",
    "        print(\"没了没了，没有酒啦~\")\n",
    "    else:\n",
    "        new_num = beer_num - 6\n",
    "        if new_num == 3:\n",
    "            print(\"只剩三瓶，不能拿了~\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bf431d9",
   "metadata": {},
   "source": [
    "# while 循环\n",
    "  while true 无限循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "74888189",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不许。。运行了 1\n",
      "不许。。运行了 2\n",
      "不许。。运行了 3\n",
      "不许。。运行了 4\n",
      "不许。。运行了 5\n"
     ]
    }
   ],
   "source": [
    "count = 0 #计数器\n",
    "while True:\n",
    "    count +=1\n",
    "    print(\"不许。。运行了\",count)\n",
    "    if count == 5:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "170fa5f4",
   "metadata": {},
   "source": [
    "* 笔记\n",
    "> 1. while循环往往和True一起使用\n",
    "> 2. while True 如果没有给定确定的条件，将无线循环下去\n",
    "> 3. while True 可以按照一定的条件执行break程序退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bb4a403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card········\n",
      "你有4次机会来玩这个游戏\n"
     ]
    }
   ],
   "source": [
    "## 猜数字\n",
    "\n",
    "# 1. 拿数字给别人看\n",
    "hide_card = getpass.getpass(\"hide_card\")\n",
    "\n",
    "# 2. 设置游戏的次数\n",
    "count = 5\n",
    "\n",
    "while True: \n",
    "    count -=1\n",
    "    # 2. 猜数字的人来猜\n",
    "    print(\"你有%s次机会来玩这个游戏\"%(count))\n",
    "    if count == 0:\n",
    "        print(\"游戏失败！\")\n",
    "        break\n",
    "    else:\n",
    "        guess_digit = input(\"请输入你猜的数字：\")\n",
    "     #情景一：猜对了\n",
    "    if hide_card == guess_digit:\n",
    "        print(\"666猜对咯\")\n",
    "        break\n",
    "    #情景二：猜错了\n",
    "    else:\n",
    "        if hide_card > guess_digit:\n",
    "            print(\"想一想？再大点！\")\n",
    "        elif hide_card < guess_digit:\n",
    "            print(\"更小喔~\")\n",
    "            \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7916292f",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
